//  on mac os:g++ -std=c++0x ./jni_threadpool_work.cpp -I./  -dynamiclib -o ../../resources/libthreadpool.dylib

#include "com_threadpool_ThreadPoolWork.h"
#include  <chrono>
#include "ThreadPool.h"
#include "stdio.h"
using namespace std;
JNIEXPORT void JNICALL Java_com_threadpool_ThreadPoolWork_startThreadPool
  (JNIEnv * env, jobject obj){
   ThreadPool _pool(2);
   for (int i = 0; i < 3; ++i) {
          _pool.add([=] {
              printf("form c plus plus test%d !!!!!!\n",i);
          });
    }

        printf("wait thread pool work done!\n");
        //current thread sleep 2 s
        std::this_thread::sleep_for (chrono::seconds(1));


       printf("开始调用java端代码\n");
       //获取obj中对象的class对象
       jclass clazz = env->GetObjectClass(obj);
       //获取Java中的number字段的id(最后一个参数是number的签名)
       jfieldID id_number = env->GetFieldID(clazz,"number","I");
       //获取number的值
       jint number = env->GetIntField(obj,id_number);
       //输出到控制台
       printf("number value:%d\n",number);
       //修改number的值为100,这里要注意的是jint对应C++是long类型,所以后面要加一个L
       env->SetIntField(obj,id_number,100L);
  }


  JNIEXPORT jstring JNICALL Java_com_threadpool_ThreadPoolWork_getName
    (JNIEnv *env, jobject){
     char buf[128]={0};
     sprintf(buf,"cplus :这是测试teststring");
     return env->NewStringUTF(buf); ;
    }

